home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Menu Controls / UTextShape.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  9.4 KB  |  394 lines  |  [TEXT/MPS ]

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // UShapes.cp
  3.  
  4. #ifndef __UTEXTSHAPE__
  5. #include "UTextShape.h"
  6. #endif
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UDIALOG__
  11. #include <UDialog.h>
  12. #endif
  13.  
  14. #ifndef __UFILE__
  15. #include <UFile.h>
  16. #endif
  17.  
  18. #ifndef __USTREAM__
  19. #include <UStream.h>
  20. #endif
  21.  
  22. #ifndef __UMACAPPUTILITIES__
  23. #include <UMacAppUtilities.h>
  24. #endif
  25.  
  26. #ifndef __UVIEW__
  27. #include <UView.h>
  28. #endif
  29.  
  30. #ifndef __UMEMORY__
  31. #include <UMemory.h>
  32. #endif
  33.  
  34. // Toolbox
  35.  
  36. #ifndef __QUICKDRAW__
  37. #include <Quickdraw.h>
  38. #endif
  39.  
  40. #ifndef __FONTS__
  41. #include <Fonts.h>
  42. #endif
  43.  
  44. // DrawShapes
  45.  
  46. #ifndef __PATTERNMENU__
  47. #include "PatternMenu.h"
  48. #endif
  49.  
  50. #ifndef __UDRAWSHAPES__
  51. #include "UDrawShapes.h"
  52. #endif
  53.  
  54. #ifndef __USHAPEVIEW__
  55. #include "UShapeView.h"
  56. #endif
  57.  
  58. //----------------------------------------------------------------------------------------
  59.  
  60. const Boolean kUseOutlineFonts = true;
  61.  
  62. const short kEditTextInset = 4;
  63.  
  64. //========================================================================================
  65. // GLOBAL Functions
  66. //========================================================================================
  67. #undef Inherited
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // HandleToString
  71. //----------------------------------------------------------------------------------------
  72. #pragma segment AClipboard
  73.  
  74. void HandleToString(Handle text, CStr255& string)
  75. {
  76.     short textLength = Min(GetHandleSize(text), 255);
  77.     string.Length() = textLength;
  78.     if (textLength > 0)
  79.         MABlockMove(*text, &string[1], textLength);
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // StringToHandle
  84. //----------------------------------------------------------------------------------------
  85. #pragma segment AClipboard
  86.  
  87. void StringToHandle(CStr255& string, Handle text)
  88. {
  89.     Size textLength = string.Length();
  90.     if (GetHandleSize(text) != textLength)
  91.     {
  92.         SetPermHandleSize(text, textLength);
  93.         FailMemError();
  94.     }
  95.     if (textLength > 0)
  96.         MABlockMove(&string[1], *text, textLength);
  97. }
  98.  
  99. //========================================================================================
  100. // CLASS TTextShape
  101. //========================================================================================
  102. #undef Inherited
  103. #define Inherited TShape
  104.  
  105. #pragma segment ARes
  106. MA_DEFINE_CLASS_M1(TTextShape, Inherited);
  107.  
  108. //----------------------------------------------------------------------------------------
  109. // TTextShape Constructor
  110. //----------------------------------------------------------------------------------------
  111. #pragma segment AOpen
  112.  
  113. TTextShape::TTextShape()
  114. {
  115.     fEditText = NULL;
  116.     fText = NULL;
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // TTextShape::ITextShape
  121. //----------------------------------------------------------------------------------------
  122. #pragma segment AOpen
  123.  
  124. void TTextShape::ITextShape(const CRect& itsExtent, short itsID)
  125. {
  126.     IShape(itsExtent, itsID);
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // TTextShape::DoInitialState
  131. //----------------------------------------------------------------------------------------
  132. #pragma segment AOpen
  133.  
  134. void TTextShape::DoInitialState(TShapeView* itsView)
  135. {
  136.     Inherited::DoInitialState(itsView);
  137.     
  138.     CreateEditText(itsView);
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // TTextShape::CreateEditText
  143. //----------------------------------------------------------------------------------------
  144. #pragma segment AOpen
  145.  
  146. void TTextShape::CreateEditText(TView* itsView)
  147. {
  148.     if (!fEditText)
  149.     {
  150.         VPoint loc(fLocation);
  151.         VPoint size(fSize);
  152.         VPoint inset(kEditTextInset, kEditTextInset);
  153.         loc += inset;
  154.         size -= inset;
  155.         size -= inset;
  156.         
  157.         fEditText = new TEditText;
  158.         fEditText->IEditText(itsView, loc, size, 255);
  159.     }
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // TTextShape::SetEditTextFrame
  164. //----------------------------------------------------------------------------------------
  165. #pragma segment AOpen
  166.  
  167. void TTextShape::SetEditTextFrame()
  168. {
  169.     if (fEditText)
  170.     {
  171.         VPoint loc(fLocation);
  172.         VPoint size(fSize);
  173.         VPoint inset(kEditTextInset, kEditTextInset);
  174.         loc += inset;
  175.         size -= inset;
  176.         size -= inset;
  177.         VRect newFrame(loc, loc + size);
  178.         fEditText->SetFrame(newFrame, kInvalidate);
  179.     }
  180. }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. // TTextShape::Clone
  184. //----------------------------------------------------------------------------------------
  185. #pragma segment AClose
  186.  
  187. TObject* TTextShape::Clone()    // Override
  188. {
  189.     TTextShape* bozo = (TTextShape*) Inherited::Clone();
  190.     FailNIL(bozo);
  191.     
  192.     bozo->fEditText = NULL;
  193.     bozo->fText = NULL;
  194.     
  195.     if (fText)
  196.     {
  197.         Size textSize = GetHandleSize(fText);
  198.         Handle textData = NewPermHandle(textSize);
  199.         FailNIL(textData);
  200.         MABlockMove(*fText, *textData, textSize);
  201.         bozo->fText = textData;
  202.     }
  203.     
  204.     return bozo;
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208. // TTextShape::Free
  209. //----------------------------------------------------------------------------------------
  210. #pragma segment AClose
  211.  
  212. void TTextShape::Free()    // Override
  213. {
  214.     fEditText = NULL; // (TEditText*) FreeIfObject(fEditText);
  215.     fText = NULL; // DisposeIfHandle(fText);
  216.  
  217.     Inherited::Free();
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. // TTextShape::ReadFrom
  222. //----------------------------------------------------------------------------------------
  223. #pragma segment AReadFile
  224.  
  225. void TTextShape::ReadFrom(TStream* aStream)    // Override
  226. {
  227.     Inherited::ReadFrom(aStream);
  228.     
  229.     Handle text = aStream->ReadHandle();
  230.     SetText(text);
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // TTextShape::WriteTo
  235. //----------------------------------------------------------------------------------------
  236. #pragma segment AWriteFile
  237.  
  238. void TTextShape::WriteTo(TStream* aStream)    // Override
  239. {
  240.     Inherited::WriteTo(aStream);
  241.     
  242.     if (fEditText)
  243.     {
  244.         CStr255    string;
  245.         fEditText->GetText(string);
  246.         if (!fText)
  247.         {
  248.             fText = NewPermHandle(string.Length());
  249.             FailNIL(fText);
  250.         }
  251.         StringToHandle(string, fText);
  252.     }
  253.     aStream->WriteHandle(fText);
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // TTextShape::SetText
  258. //----------------------------------------------------------------------------------------
  259. #pragma segment AClipboard
  260.  
  261. void TTextShape::SetText(Handle text)
  262. {
  263.     if (fText != text)
  264.     {
  265.         fText = DisposeIfHandle(fText);
  266.         fText = text;
  267.     }
  268.     
  269.     if (!fEditText)
  270.         CreateEditText(NULL);
  271.     
  272.     if (fEditText)
  273.     {
  274.         CStr255    string;
  275.         HandleToString(fText, string);
  276.         fEditText->SetText(string, kDontRedraw);
  277.     }
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // TTextShape::BeInView
  282. //----------------------------------------------------------------------------------------
  283. #pragma segment ARes
  284.  
  285. void TTextShape::BeInView(TShapeView* itsView)    // Override
  286. {
  287.     //    Inherited::BeInView(itsView);
  288.     
  289.     if (!fEditText)
  290.         CreateEditText(itsView);
  291.     else
  292.         itsView->AddSubView(fEditText);
  293.     
  294.     if (fText)
  295.         SetText(fText);
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // TTextShape::Draw
  300. //----------------------------------------------------------------------------------------
  301. #pragma segment ARes
  302.  
  303. void TTextShape::Draw()    // Override
  304. {
  305.     CRect extent;
  306.     GetFrame(extent);
  307.     
  308.     PenNormal();
  309.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  310.     {
  311.         // Get the color of the menu item representing the shape's color
  312.         RGBForeColor(fColor);
  313.         DrawInsides(extent);
  314.         ForeColor(blackColor);
  315.     }
  316.     else
  317.         DrawInsides(extent);
  318.     
  319.     if (!fEditText)
  320.     {
  321.         const CStr255 s = "T";
  322.     
  323.         TextFont(geneva);
  324.         TextFace(normal);
  325.         TextSize(9);
  326.         TextMode(srcOr);
  327.     
  328.         short x = (extent.left + extent.right) / 2;
  329.         short w = StringWidth(s) / 2;
  330.         CRect r(x - w, extent.bottom - 14, x + w, extent.bottom - 4);
  331.         EraseRect(r);
  332.         MADrawString(s, r, teCenter, kUseOutlineFonts);
  333.     }
  334.     
  335.     DrawOutline();
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. // TTextShape::DrawInsides
  340. //----------------------------------------------------------------------------------------
  341. #pragma segment ARes
  342.  
  343. void TTextShape::DrawInsides(const CRect& extent)
  344. {
  345.     CRect r(extent);
  346.     
  347.     r.bottom = extent.top + kEditTextInset;
  348.     FillRect(r, &gPat[fPattern]);
  349.     r.bottom = extent.bottom;
  350.     
  351.     r.right = extent.left + kEditTextInset;
  352.     FillRect(r, &gPat[fPattern]);
  353.     r.right = extent.right;
  354.     
  355.     r.left = extent.right - kEditTextInset;
  356.     FillRect(r, &gPat[fPattern]);
  357.     r.left = extent.left;
  358.     
  359.     r.top = extent.bottom - kEditTextInset;
  360.     FillRect(r, &gPat[fPattern]);
  361.     r.top = extent.top;
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. // TTextShape::DrawOutline
  366. //----------------------------------------------------------------------------------------
  367. #pragma segment ARes
  368.  
  369. void TTextShape::DrawOutline()
  370. {
  371.     PenSize(1, 1);
  372.     CRect extent;
  373.     GetFrame(extent);
  374.     FrameRect(extent);
  375. }
  376.  
  377. //----------------------------------------------------------------------------------------
  378. // TTextShape::SetFrame
  379. //----------------------------------------------------------------------------------------
  380. #pragma segment AClipboard
  381.  
  382. void TTextShape::SetFrame(const CRect& extentRect) // override
  383. {
  384.     CRect oldFrame;
  385.     GetFrame(oldFrame);
  386.     if (oldFrame != extentRect)
  387.     {
  388.         Inherited::SetFrame(extentRect);
  389.         if (fEditText)
  390.             SetEditTextFrame();
  391.     }
  392. }
  393.  
  394.